#!/bin/sh

: "${YTFZF_GUI_CSS:=$YTFZF_CONFIG_DIR/interface-gui.css}"

display_text_gui () {
    MAIN_DIALOG="
<window>
    <edit>
	<width>500</width>
	<height>500</height>
	<default>$*</default>
    </edit>
</window>" gtkdialog
}

info_wait_prompt_gui () {
    :
}

gui_dialog () {
    css_file="${session_temp_dir}/gtk.css"
    if [ -f "$YTFZF_GUI_CSS" ]; then
        css_file="$YTFZF_GUI_CSS"
    else
        : > "$css_file"
    fi
    export MAIN_DIALOG="<window><vbox scrollable=\"true\" vscrollbar-policy=\"0\">"
    urls="$(jq -r '.url')"
    while read -r url; do
        _correct_json="$(jq -r --arg url "$url" '[.[]|select(.url==$url)]|unique_by(.ID)[0]' < "$video_json_file")"
        id="$(_get_video_json_attr "ID")"
        title="$(_get_video_json_attr "title")"
        channel="$(_get_video_json_attr "channel")"
        views="$(_get_video_json_attr "views" | add_commas)"
        date="$(_get_video_json_attr "date")"
        scraper="$(_get_video_json_attr "scraper")"
        duration="$(_get_video_json_attr "duration")"
        description="$(_get_video_json_attr "description" | sed 's/\\n/\n/g')"


        unset IFS

        for path in "${YTFZF_CUSTOM_THUMBNAILS_DIR}/$id.jpg" "${thumb_dir}/${id}.jpg"  "${YTFZF_CUSTOM_THUMBNAILS_DIR}/YTFZF:DEFAULT.jpg"; do
            thumb_path="$path"
            [ -f "${thumb_path}" ] && break
        done

        export MAIN_DIALOG="$MAIN_DIALOG
        <hbox>
            <vbox>
                <text name=\"TitleText\">
                    <label>$title (@$channel)</label>
                </text>
                <text name=\"ViewsText\">
                    <label>$views views</label>
                </text>
                <text name=\"DateText\">
                    <label>uploaded: $date</label>
                </text>
                <text name=\"DurationText\">
                    <label>duration: $duration</label>
                </text>
                <button name=\"UrlButton\">
                    <label>$url</label>
                </button>
            </vbox>
            <pixmap name=\"Thumbnail\">
                <width>400</width>
                <input file>$thumb_path</input>
            </pixmap>
        </hbox>"
    done <<-EOF
    $urls
EOF

    export MAIN_DIALOG="$MAIN_DIALOG</vbox></window>"
    if gtkdialog -h 2> /dev/null | grep -q -- '--styles'; then
        MAIN_DIALOG=$MAIN_DIALOG gtkdialog --styles "$css_file"
    else

        MAIN_DIALOG=$MAIN_DIALOG gtkdialog
    fi
}

interface_gui () {
    video_json_file="$1"
    selected_id_file="$2"
    command_exists "gtkdialog" || die 3 "gtkdialog (https://github.com/puppylinux-woof-CE/gtkdialog) is required for the gui interface\n"
    create_sorted_video_data | gui_dialog | sed -n -e 's/EXIT="\(.*\)"/\1/p' -e 's/[[:space:]]*//g' > "$selected_id_file"
    while read -r line; do
        [ "$line" = "abort" ] && exit 0
    done < "$selected_id_file"
}

print_help_gui (){
    print_info "YTFZF_GUI_CSS is a file for custom css for the window\nselectors:\n#TitleText\n#ViewsText\n#DateText\n#DurationText\n#UrlButton\n#Thumbnail\n"
    exit 0
}

search_prompt_menu_gui () {
    vars=$(MAIN_DIALOG="
    <window>
	<vbox>
	    <entry>
		<default>Search...</default>
		<variable>SEARCH</variable>
	    </entry>
	    <button>
		<label>Search!</label>
	    </button>
	</vbox>
    </window>" gtkdialog)

    while read -r line; do
	case "$line" in
	    *SEARCH*)
		_search=$(printf "%s" "$line" | tr -d '"')
		_search="${_search#*=}" ;;
	    EXIT*) break ;;
	esac
    done <<-EOF
    $vars
EOF
}
